package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.SearchPrepayInfo;
import com.vogue.supplymall.common.domain.dto.UpdatePrepay;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.exception.ParamErrorException;
import com.vogue.supplymall.order.common.CheckPrepayInfoUtils;
import com.vogue.supplymall.order.domain.biz.PrepaySearchBiz;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Prepay search controller.
 *
 * @author zhangliang
 * @date Create in 2018-08-01
 */
@RestController
public class PrepaySearchController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger log = LoggerFactory.getLogger(PrepaySearchController.class);
    @Resource
    private PrepaySearchBiz prepaySearchBiz;

    /**
     * Search prepay for organ response entity.
     * 预付货款管理-我的供货商
     *
     * @param appver           the appver
     * @param searchPrepayInfo the search prepay info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/prepay/organ", method = RequestMethod.POST)
    public ResponseEntity<Object> searchPrepayForOrgan(@PathVariable("appver") String appver, @RequestBody SearchPrepayInfo searchPrepayInfo) {
        log.info("search prepay organ start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            if (null != info.getOrganInfo()) {
                searchPrepayInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchPrepayInfo.setOrganid(info.getLoginId());
            }
            searchPrepayInfo.setSyscodeid(info.getSyscodeid());
            searchPrepayInfo.setAdmin(info.isAdmin());
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(prepaySearchBiz.searchPrepayForOrgan(searchPrepayInfo));
        resultInfo.setMsg("");
        log.info(" <<<<<< search prepay organ  end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Search prepay for shop response entity.
     * 预付货款管理-我的供货商-供货商店铺
     *
     * @param appver           the appver
     * @param searchPrepayInfo the search prepay info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/prepay/organ/store", method = RequestMethod.POST)
    public ResponseEntity<Object> searchPrepayForShop(@PathVariable("appver") String appver, @RequestBody SearchPrepayInfo searchPrepayInfo) {
        log.info("search prepay shop start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            if (null != info.getOrganInfo()) {
                searchPrepayInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchPrepayInfo.setOrganid(info.getLoginId());
            }
            searchPrepayInfo.setSyscodeid(info.getSyscodeid());
            searchPrepayInfo.setAdmin(info.isAdmin());
        } else {
            log.info("获取登信息失败！");
        }

        if (null == searchPrepayInfo.getSellsyscodeid()) {
            throw new ParamErrorException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(prepaySearchBiz.searchPrepayForShop(searchPrepayInfo));
        resultInfo.setResult(true);
        resultInfo.setMsg("");
        log.info(" <<<<<< search prepay shop  end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Search prepay for shop detail response entity.
     * 预付货款管理-我的供货商-供货商店铺-预付货款消费明细
     *
     * @param appver           the appver
     * @param searchPrepayInfo the search prepay info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/prepay/organ/store/detail", method = RequestMethod.POST)
    public ResponseEntity<Object> searchPrepayForShopDetail(@PathVariable("appver") String appver, @RequestBody SearchPrepayInfo searchPrepayInfo) {
        log.info("search prepay shop detail start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            if (null != info.getOrganInfo()) {
                searchPrepayInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchPrepayInfo.setOrganid(info.getLoginId());
            }
            searchPrepayInfo.setSyscodeid(info.getSyscodeid());
        } else {
            log.info("获取登信息失败！");
        }

        if (null == searchPrepayInfo.getSellsyscodeid() || null == searchPrepayInfo.getSellorganid()) {
            throw new ParamErrorException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(prepaySearchBiz.searchPrepayForShopDetail(searchPrepayInfo));
        resultInfo.setResult(true);
        resultInfo.setMsg("");
        log.info(" <<<<<< search prepay shop detail end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    /**
     * Search prepay balance response entity.
     * 查询预付货款余额
     *
     * @param appver           the appver
     * @param searchPrepayInfo the search prepay info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/prepay/search/balance", method = RequestMethod.POST)
    public ResponseEntity<Object> searchPrepayBalance(@PathVariable("appver") String appver, @RequestBody SearchPrepayInfo searchPrepayInfo) {
        log.info("search prepay balance start >>>>>> ");

        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            if (null != info.getOrganInfo()) {
                searchPrepayInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchPrepayInfo.setOrganid(info.getLoginId());
            }
            searchPrepayInfo.setSyscodeid(info.getSyscodeid());
        } else {
            log.info("获取登信息失败！");
        }

        if (null == searchPrepayInfo.getSellsyscodeid() || null == searchPrepayInfo.getSellorganid()) {
            throw new ParamErrorException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(prepaySearchBiz.searchPrepayBalance(searchPrepayInfo));
        resultInfo.setResult(true);
        resultInfo.setMsg("");

        log.info(" <<<<<< search prepay balance end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Search prepay balance for cld response entity.
     * 查询预付款余额--仅限云供应链查询使用
     *
     * @param appver       the appver
     * @param updatePrepay the update prepay
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/order/prepay/cld/search/balance", method = RequestMethod.POST)
    public ResponseEntity<Object> searchPrepayBalanceForCld(@PathVariable("appver") String appver, @RequestBody UpdatePrepay updatePrepay) {
        log.info("search prepay balance for cld start >>>>>> ");
        if (CheckPrepayInfoUtils.checkSearchPrepayForCldParameter(updatePrepay)) {
            throw new ParamErrorException("");
        }
//        if(updatePrepay.getOrigin() != 2) {
//            throw new ParamErrorException("");
//        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(prepaySearchBiz.searchPrepayBalanceForCld(updatePrepay));
        resultInfo.setResult(true);
        resultInfo.setMsg("");
        log.info(" <<<<<< search prepay balance for cld end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
